<?php

class RecomTypeAction extends Action
{
    public function index() {
        $S = M("recom_type_info");
        $List  = $S->select();

        $this->assign('list' , $List);
        $this->display();
    }

    public function recomAdd(){
        $recomName = $_POST['recom_type_name'];
        $recom_type_code = $_POST['recom_type_code'];

        $M = M('recom_type_info');

        if( !empty($recomName) &&  empty($recom_type_code) ){
            $Form = D("recom_type_info");
            if ( $Form->create() ) {
                if ( false !== $Form->add() ) {
                    $this->assign("jumpUrl","index");
                    $this->success('数据添加成功！');
                } else {
                    $this->assign("jumpUrl","index");
                    $this->error('数据写入错误');
                }
            }
            exit;
        }elseif( !empty($recom_type_code) ){
            if ($vo = $M->create()) {
                $data['recom_type_code'] = $recom_type_code;
                $data['recom_type_name'] = $_POST['recom_type_name'];
                $list = $M->save($data);
            }
            if ($list !== false) {
                $this->assign("jumpUrl","index");
                $this->success('数据更新成功！');
            } else {
                $this->assign("jumpUrl","index");
                $this->error("没有更新任何数据!");
            }
            exit;
        }

        $editCode = $_GET['recom_type_code'];
        $editInfo = $M->where("recom_type_code=$editCode")->select();

        $this->assign('editCode' , $editCode);
        $this->assign('editInfo' , $editInfo);
        $this->display('recomadd');
    }

    public function recomDel(){

        $recom_type_code = $_GET['recom_type_code'];
        $M = M('recom_type_info');
        $M->where("recom_type_code=$recom_type_code")->delete();

        $list = $M->select();
        $this->assign( 'list' , $list );
        $this->display('index');
    }

    public function recomList(){
        $S = M('recom_base_info as a');
        $List = $S->join('24_recom_type_info as b ON b.recom_type_code = a.recom_site')->select();
        $this->assign('list' , $List);
        $this->display('recomlist');
    }

    public function recomSet(){

        import("ORG.Net.UploadFile");

        $recomPhoto = $_FILES['recom_photo']['name'];
        $recom_code = $_POST['recom_code'];

        if( !empty($recomPhoto) ){
            $upload = new UploadFile();
            $upload->maxSize  = '3145728' ;
            $upload->allowExts  = array('jpg', 'gif', 'png', 'jpeg');
            $upload->savePath =  "./Public/Uploads/Images/";
            $upload->saveRule = 'images_'.time();

            if( !$upload->upload() ) {
                $this->error($upload->getErrorMsg());
            }else{
                $photoInfo = $upload->getUploadFileInfo();
            }
        }

        $M = M('recom_base_info');
        $data['recom_site'] = $_POST['recom_site'];
        $data['recom_link'] = str_replace('http://', '', $_POST['recom_link']);
        $data['recom_type'] = $_POST['recom_type'];
        $data['recom_title'] = $_POST['recom_title'];
        $data['recom_width'] = $_POST['recom_width'];
        $data['recom_height'] = $_POST['recom_height'];

        if( !empty($recomPhoto) &&  empty($recom_code) ){
            $Form = D("recom_base_info");
            if ( $Form->create() ) {
                if( !empty($photoInfo) ){
                    $data['recom_photo_name'] = $photoInfo[0]['savename'];
                }
                if ( false !== $Form->add($data) ) {
                    $this->assign("jumpUrl","recomList");
                    $this->success('数据添加成功！');
                } else {
                    $this->assign("jumpUrl","recomList");
                    $this->error('数据写入错误');
                }
            }
            exit;
        }elseif( !empty($recom_code) ){
            if ($vo = $M->create()) {
                $data['recom_code'] = $recom_code;
                if( !empty($photoInfo) ){
                    $oldPicPath = $M->where("recom_code=$recom_code")->getField('recom_photo_name');
                    $data['recom_photo_name'] = $photoInfo[0]['savename'];
                }
                $list = $M->save($data);
            }
            if ($list !== false) {
                if( !empty($oldPicPath) ){
                    @unlink("./Public/Uploads/Images/".$oldPicPath);
                }
                $this->assign("jumpUrl","recomList");
                $this->success('数据更新成功！');
            } else {
                $this->assign("jumpUrl","recomList");
                $this->error("没有更新任何数据!");
            }
            exit;
        }

		$editCode = $_GET['recom_code'];
        $editInfo = $M->where("recom_code=$editCode")->select();

        $S = M("recom_type_info");
        $recomTypeList = $S->select();
		$this->assign('editInfo' , $editInfo);
		$this->assign('editCode' , $editCode);
        $this->assign('recomTypeList' , $recomTypeList);
        $this->display('recomset');
    }

	public function reDel(){

        $recom_code = $_GET['recom_code'];
        $M = M('recom_base_info');
        $PicPath = $M->where("recom_code=$recom_code")->getField('recom_photo_name');
        $result = $M->where("recom_code=$recom_code")->delete();

        if( $result ){
            @unlink("./Public/Uploads/Images/".$PicPath);
            $this->assign("jumpUrl","__URL__/recomlist");
            $this->success('删除成功！');
        }
    }

}
?>